home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / MacStarter Pascal 1.0 / xWindows definition files / xExpressionInput.p < prev    next >
Encoding:
Text File  |  1993-12-11  |  3.8 KB  |  112 lines  |  [TEXT/PJMM]

  1. unit xExpressionInput;
  2.  
  3. { This unit defines two subclasses of xStringInput which are used for inputting }
  4. { mathematical expressions, as defined in the UNIT "expression"  }
  5.  
  6. interface
  7.  
  8. uses
  9.     xWindow, xInputDecoration, expression;
  10.  
  11. type
  12.     xExpressionInput = object(xStringInput)
  13.            { Represents an input box to be used to type in a mathematical expression. }
  14.             displayAlertOnError: boolean;  { if this is true, then the user is informed when }
  15.                    { an error is found in the expression during a call to GetExpression; this }
  16.                    { is the default response. }
  17.             procedure SetUp (win: xWindow;
  18.                                         theLeft, theTop, theWidth, theHeight: integer);
  19.             override;
  20.            { create an expression box and install it in the specified window; the remaining }
  21.            { parameters determine the location and size of the input box--see the commenst }
  22.            { on the definition of this procedure in the calss xStringInput, and also see }
  23.            { the comment on the definiton of procedure Install in the class xWindowDecoration }
  24.             procedure GetExpression (var exp: expression;
  25.                                         var err: boolean);
  26.            { gets the expression typed in by the user, converted to an object of type }
  27.            { expression (see the definition of this class in file expression.p); if the }
  28.            { contents of the input box do not represent a legal expression, then the }
  29.            { value of err is set to true, and exp is set to NIL--also, the user is informed }
  30.            { of the error unless you set the value of DisplayAlertOnError to false. }
  31.             procedure doKey (ch: char;
  32.                                         modifiers: longint);
  33.             override;
  34.           { modifies doKey to reject illegal characters. }
  35.         end;
  36.  
  37.     xConstantExpressionInput = object(xExpressionInput)
  38.           { allows the user to input a constant expression (i.e., one involving no }
  39.           { variables. }
  40.             procedure GetValue (var val: extended;
  41.                                         var err: boolean);
  42.           { Retrieve the value of the expression typed in by the user; if the contents of }
  43.           { the input box do not form a legal constant expression, then the value of Err is }
  44.           { set to true--also, the user is informed of the error unless you set the value }
  45.           { of DisplayAlertOnError to false.}
  46.         end;
  47.  
  48.  
  49. implementation
  50.  
  51. procedure xExpressionInput.GetExpression (var exp: expression;
  52.                                 var err: boolean);
  53.     var
  54.         str: string;
  55.         errPos: integer;
  56.         errMessage: string;
  57.     begin
  58.         new(exp);
  59.         GetContents(str);
  60.         exp.CreateFromString(str, errPos, errMessage);
  61.         if errPos > -1 then begin
  62.                 err := true;
  63.                 if displayAlertOnError then begin
  64.                         TellUser(StringOf('Error trying to read expression: ', errMessage));
  65.                         select;
  66.                         TESetSelect(errPos, errPos, TE);
  67.                     end;
  68.                 dispose(exp);
  69.             end
  70.         else
  71.             err := false;
  72.     end;
  73.  
  74. procedure xExpressionInput.SetUp (win: xWindow;
  75.                                 theLeft, theTop, theWidth, theHeight: integer);
  76.     begin
  77.         inherited setUp(win, theLeft, theTop, theWidth, theHeight);
  78.         displayAlertOnError := true;
  79.     end;
  80.  
  81. procedure xExpressionInput.doKey (ch: char;
  82.                                 modifiers: longint);
  83.     begin
  84.         if not wantsKey then
  85.             EXIT(doKey);
  86.         if ch in ['a'..'z', 'A'..'Z', '_', ' ', '[', ']', '{', '}', '(', ')', ',', '.', '+', '-', '*', '/', '^', 'π', '0'..'9', chr(8), chr(9), chr($1C)..chr($1F)] then
  87.             inherited doKey(ch, modifiers)
  88.         else
  89.             Sysbeep(5);
  90.     end;
  91.  
  92.  
  93. procedure xConstantExpressionInput.GetValue (var val: extended;
  94.                                 var err: boolean);
  95.     var
  96.         exp: expression;
  97.     begin
  98.         val := errorVal;
  99.         GetExpression(exp, err);
  100.         if not err then begin
  101.                 if exp.isConstant then
  102.                     val := exp.value
  103.                 else if DisplayAlertOnError then begin
  104.                         TellUser('You must enter a constant, or a constant expression.');
  105.                         select;
  106.                         TESetSelect(0, 32000, TE);
  107.                     end;
  108.                 exp.kill;
  109.             end;
  110.     end;
  111.  
  112. end.